﻿using Abp.Domain.Repositories;
using Abp.Domain.Services;
using Abp.Events.Bus.Handlers;
using AbpProjectTemplate.Components.Resources.Event;
using System;
using System.IO;
using System.Threading.Tasks;

namespace AbpProjectTemplate.Components.Resources
{
    public class RescoureUploadManager : DomainService
        , IAsyncEventHandler<ResourceUploadedEventData>
        , IAsyncEventHandler<UploadResourceRemoveEventData>
    {
        private readonly IRepository<UploadResource, Guid> _repository;

        public RescoureUploadManager(IRepository<UploadResource, Guid> repository)
        {
            this._repository = repository;
        }

        public async Task Save(string folder, byte[] fileContent, string fileName = null)
        {
            var folderPath = $"";
            var fielPath = "";
            await Task.CompletedTask;
            File.WriteAllBytes(fielPath, fileContent);
        }


        public async Task HandleEventAsync(ResourceUploadedEventData eventData)
        {
            var resource = new UploadResource()
            {
             
                Path = eventData.Path,
                Url = eventData.Url
            };
            await this._repository.InsertAsync(resource);
        }

        public async Task HandleEventAsync(UploadResourceRemoveEventData eventData)
        {
            await this._repository.DeleteAsync(t => t.Url == eventData.FileUrl);
        }
    }
}
